<?php
	//引入配置文件和数据操作函数
	require '../../public/config.php';
	require '../../public/mysql.php';
	//接受操作


	$action = $_GET['a'];
	//判断是哪一个操作
	switch ($action) {
		case 'add':
			//获取表单提交的数据
			$cname = $_POST['name'];
			$pid = $_POST['pid'];
			//连接数据库
			$link = connect();
			//准备sql和执行sql
			if ($link['error']) {
				$sql = "insert into category(name,pid) value('{$cname}','{$pid}')";
				$result = excute($link['mess'], $sql);
				
				if ($result['error']) {
					echo "<script>alert('添加成功');location.href='./cateList.php'</script>";
				}
			}
		break;
		case 'edit':
			//接受传递过来的当前记录的id
			$id = $_POST['id'];
			$pid = $_POST['pid'];
			$cname = $_POST['name']; 
			//连接数据库
			$link = connect();
			//准备sql和执行sql
			if ($link['error']) {
				//准备sql
				$sql = "update category set pid = '{$pid}', name = '{$cname}' where id ='{$id}'";
				$result = excute($link['mess'], $sql);
				if ($result['error']) {
					echo "<script>alert('修改成功');location.href='./cateList.php'</script>";
				}
			}
		break;
		//删除操作
//		case 'del':
//			//接受传递过来当前记录的id
//			$id = $_GET['id'];
//			//连接数据库
//			$link = connect();
//			//准备sql和执行sql
//              $sql="select * from category where pid=$id";
//              $result = excute($link['mess'], $sql);
//              if($result['error']){
//			     echo "<script>alert('有子类无法删除');location.href='./cateList.php'</script>";
//			     die;
//			  }
//		     
//			if ($link['error']) {
//				//准备sql
//				$sql = "delete from category where id ='{$id}'";
//				$result = excute($link['mess'], $sql);
//				if ($result['error']) {
//					echo "<script>alert('删除成功');location.href='./cateList.php'</script>";
//				}
//			}
//		break;
case 'del':
			//接受传递过来当前记录的id
			$id = $_GET['id'];
			//连接数据库
			$link = connect();
			//准备sql和执行sql
			if ($link['error']) {
				$sql="select * from category where pid=$id";
				$result = query($link['mess'], $sql);
				if(!empty($result)){
					echo "<script>alert('有子类无法删除');location.href='./cateList.php'</script>";
					die();
				}
				//准备sql 
				$sql = "delete from category where id ='{$id}'";
				$result = excute($link['mess'], $sql);
				if ($result['error']) {
					echo "<script>alert('删除成功');location.href='./cateList.php'</script>";
				}
			}
		break; 

	}
?>